package com.cloudm.technician.ui.modules.cloudm_box.transfer

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.data.model.SearchInfo
import com.cloudm.technician.ui.base.BaseDialogFragment

/**
 * 作者: wesley
 * 时间: 2017/12/25
 * 描述: 移交的对话框
 */
class TransferDialogFragment : BaseDialogFragment(), DialogInterface.OnClickListener {

    private var info: DeviceInfo? = null
    private var searchInfo: SearchInfo? = null
    var listener: OnDialogItemClickListener? = null

    companion object {
        fun newInstance(info: DeviceInfo?,
                        searchInfo: SearchInfo) = TransferDialogFragment().apply {
            arguments = Bundle().apply {
                putParcelable("info", info)
                putParcelable("searchInfo", searchInfo)
            }
        }
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
        searchInfo = bundle.getParcelable("searchInfo")
    }

    override fun createDialog(): Dialog {
        return AlertDialog.Builder(mContext).setMessage(
                mContext.resources.getString(R.string.make_sure_trasnfer, info?.deviceName,
                        searchInfo?.name)).setPositiveButton(R.string.ok,
                this).setNegativeButton(R.string.cancel, this).create()
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }


    override fun onClick(dialog: DialogInterface?, which: Int) {
        when (which) {
            -1 -> listener?.onTransferClick(info?.id ?: -1, searchInfo?.memberId ?: -1)
        }
    }

    interface OnDialogItemClickListener {
        fun onTransferClick(deviceId: Int, memberId: Int)
    }
}